package com.dongyu.tinymap.slice;

import com.dongyu.tinymap.ResourceTable;
import com.dongyu.tinymap.TinyMap;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.IDialog;
import ohos.agp.window.dialog.ListDialog;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // obtain the map object
        TinyMap reachMap = (TinyMap) findComponentById(ResourceTable.Id_map);

        // zoom in
        Button btnZoomIn = (Button) findComponentById(ResourceTable.Id_btn_zoomin);
        btnZoomIn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                reachMap.zoomIn();
            }
        });

        // zoom out
        Button btnZoomOut = (Button) findComponentById(ResourceTable.Id_btn_zoomout);
        btnZoomOut.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                reachMap.zoomOut();
            }
        });

        // change base map
        Button btnChangeBaseMapType = (Button) findComponentById(ResourceTable.Id_btn_changebasemaptype);
        btnChangeBaseMapType.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                ListDialog listDialog = new ListDialog(MainAbilitySlice.this, ListDialog.SINGLE);
                listDialog.setItems(new String[]{"高德地图 - 道路", "高德地图 - 矢量", "高德地图 - 栅格", "Google地图 - 矢量", "Google地图 - 栅格"});
                listDialog.setOnSingleSelectListener(new IDialog.ClickedListener() {
                    @Override
                    public void onClick(IDialog iDialog, int i) {
                        if (i == 0)
                            reachMap.setMapSource(TinyMap.MapSource.GAODE_ROAD);
                        if (i == 1)
                            reachMap.setMapSource(TinyMap.MapSource.GAODE_VECTOR);
                        if (i == 2)
                            reachMap.setMapSource(TinyMap.MapSource.GAODE_SATELLITE);
                        if (i == 3)
                            reachMap.setMapSource(TinyMap.MapSource.GOOGLE_VECTOR);
                        if (i == 4)
                            reachMap.setMapSource(TinyMap.MapSource.GOOGLE_SATELLITE);
                        reachMap.refreshMap();
                        listDialog.hide();
                    }
                });
                listDialog.setButton(0, "取消", new IDialog.ClickedListener() {
                    @Override
                    public void onClick(IDialog iDialog, int i) {
                        listDialog.hide();
                    }
                });
                listDialog.setSize(600, 600);
                listDialog.show();
            }
        });

        // add element on the map
        Button btnAddElement = (Button) findComponentById(ResourceTable.Id_btn_addElement);
        btnAddElement.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                reachMap.addElement(12956517.35f, 4864667.87f, ResourceTable.Media_dot);
            }
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
